package com.lagou.wwyan.rpc.config;

import com.lagou.wwyan.rpc.client.RPCBoot;
import com.lagou.wwyan.rpc.client.RpcConsumer;
import com.lagou.wwyan.rpc.register.RpcRegister;
import com.lagou.wwyan.rpc.register.RpcRegisterFactory;
import com.lagou.wwyan.rpc.register.ZookeeperRpcRegister;
import com.lagou.wwyan.rpc.register.ZookeeperRpcRegisterFactory;
import com.lagou.wwyan.rpc.service.UserService;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RPCClientConfig {
    @Value("${zookeeper.url:127.0.0.1:2181}")
    private String zookeeperUrl;

    @Bean
    public RpcRegisterFactory rpcRegisterFactory(){
        ZookeeperRpcRegisterFactory factory = new ZookeeperRpcRegisterFactory();
        factory.setUrl(zookeeperUrl);
        return factory;
    }
    @Bean
    public RpcConsumer rpcConsumer(RpcRegisterFactory factory){
        Map<String,Object> map = new HashMap<>();
        Class<UserService> aClass = UserService.class;
        map.put(aClass.getName(),aClass);
        RpcConsumer consumer = new RpcConsumer(factory.getRegister(),map);
        return consumer;
    }

}
